<?php

namespace Registro\Frontend\Controllers;

use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

use Registro\Models\Objects;

class ObjectsController extends ControllerBase {

    /**
     * Index action
     */
    public function indexAction()
    {
        $this->persistent->parameters = null;
    }

    /**
     * Searches for reg_objects
     */
    public function searchAction()
    {

        $numberPage = 1;
        if( $this->request->isPost() )
        {
            $query = Criteria::fromInput( $this->di, "Registro\Models\Objects", $_POST );
            $this->persistent->parameters = $query->getParams();
        }
        else
        {
            $numberPage = $this->request->getQuery( "page", "int" );
        }

        $parameters = $this->persistent->parameters;
        if( !is_array( $parameters ) )
        {
            $parameters = array();
        }
        $parameters["order"] = "object_id";

        $reg_objects = Objects::find( $parameters );
        if( count( $reg_objects ) == 0 )
        {
            $this->flash->notice( "The search did not find any reg_objects" );

            return $this->dispatcher->forward( array(
                        "controller" => "objects",
                        "action" => "index"
            ) );
        }

        $paginator = new Paginator( array(
            "data" => $reg_objects,
            "limit" => 10,
            "page" => $numberPage
        ) );

        $this->view->page = $paginator->getPaginate();
    }

    /**
     * Displays the creation form
     */
    public function newAction()
    {
        
    }

    /**
     * Edits a reg_object
     *
     * @param string $object_id
     */
    public function editAction( $object_id )
    {

        if( !$this->request->isPost() )
        {

            $reg_object = Objects::findFirstByobject_id( $object_id );
            if( !$reg_object )
            {
                $this->flash->error( "reg_object was not found" );

                return $this->dispatcher->forward( array(
                            "controller" => "reg_objects",
                            "action" => "index"
                ) );
            }

            $this->view->object_id = $reg_object->object_id;

            $this->tag->setDefault( "object_id", $reg_object->object_id );
            $this->tag->setDefault( "object_name", $reg_object->object_name );
            $this->tag->setDefault( "objects_manufacturer_id", $reg_object->objects_manufacturer_id );
            $this->tag->setDefault( "object_modified", $reg_object->object_modified );
            $this->tag->setDefault( "object_modified_by", $reg_object->object_modified_by );
            $this->tag->setDefault( "object_created", $reg_object->object_created );
            $this->tag->setDefault( "object_created_by", $reg_object->object_created_by );
        }
    }

    /**
     * Creates a new Object
     */
    public function createAction()
    {
        if( !$this->request->isPost() )
        {
            return $this->dispatcher->forward( array(
                        "controller" => "objects",
                        "action" => "index"
            ) );
        }

        $reg_object = new Objects();

        $reg_object->object_id = $this->request->getPost( "object_id" );
        $reg_object->object_name = $this->request->getPost( "object_name" );
        $reg_object->objects_manufacturer_id = $this->request->getPost( "objects_manufacturer_id" );
        $reg_object->object_modified = $this->request->getPost( "object_modified" );
        $reg_object->object_modified_by = $this->request->getPost( "object_modified_by" );
        $reg_object->object_created = $this->request->getPost( "object_created" );
        $reg_object->object_created_by = $this->request->getPost( "object_created_by" );


        if( !$reg_object->save() )
        {
            foreach( $reg_object->getMessages() as $message ){
                $this->flash->error( $message );
            }

            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "new"
            ) );
        }

        $this->flash->success( "reg_object was created successfully" );

        return $this->dispatcher->forward( array(
                    "controller" => "reg_objects",
                    "action" => "index"
        ) );
    }

    /**
     * Saves a reg_object edited
     *
     */
    public function saveAction()
    {

        if( !$this->request->isPost() )
        {
            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "index"
            ) );
        }

        $object_id = $this->request->getPost( "object_id" );

        $reg_object = Objects::findFirstByobject_id( $object_id );
        if( !$reg_object )
        {
            $this->flash->error( "reg_object does not exist " . $object_id );

            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "index"
            ) );
        }

        $reg_object->object_id = $this->request->getPost( "object_id" );
        $reg_object->object_name = $this->request->getPost( "object_name" );
        $reg_object->objects_manufacturer_id = $this->request->getPost( "objects_manufacturer_id" );
        $reg_object->object_modified = $this->request->getPost( "object_modified" );
        $reg_object->object_modified_by = $this->request->getPost( "object_modified_by" );
        $reg_object->object_created = $this->request->getPost( "object_created" );
        $reg_object->object_created_by = $this->request->getPost( "object_created_by" );


        if( !$reg_object->save() )
        {

            foreach( $reg_object->getMessages() as $message ){
                $this->flash->error( $message );
            }

            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "edit",
                        "params" => array( $reg_object->object_id )
            ) );
        }

        $this->flash->success( "reg_object was updated successfully" );

        return $this->dispatcher->forward( array(
                    "controller" => "reg_objects",
                    "action" => "index"
        ) );
    }

    /**
     * Deletes a reg_object
     *
     * @param string $object_id
     */
    public function deleteAction( $object_id )
    {

        $reg_object = Objects::findFirstByobject_id( $object_id );
        if( !$reg_object )
        {
            $this->flash->error( "reg_object was not found" );

            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "index"
            ) );
        }

        if( !$reg_object->delete() )
        {

            foreach( $reg_object->getMessages() as $message ){
                $this->flash->error( $message );
            }

            return $this->dispatcher->forward( array(
                        "controller" => "reg_objects",
                        "action" => "search"
            ) );
        }

        $this->flash->success( "reg_object was deleted successfully" );

        return $this->dispatcher->forward( array(
                    "controller" => "reg_objects",
                    "action" => "index"
        ) );
    }
}